#include "aircondition_dialog.h"
#include "ui_aircondition_dialog.h"

Aircondition_Dialog::Aircondition_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Aircondition_Dialog), currentMode(1), currentFanSpeed(2), currentTemp(25) ,isPowerOn(false)
{
    ui->setupUi(this);
    
    //温度显示
    ui->tempLabel->setText(QString::number(currentTemp) + "℃");
    ui->tempLabel->setAlignment(Qt::AlignCenter);
    QFont tempFont = ui->tempLabel->font();
    tempFont.setPointSize(48);
    ui->tempLabel->setFont(tempFont);
    connect(ui->UpButton , &QPushButton::clicked , this , &Aircondition_Dialog::increaseTemp);
    connect(ui->DownButton , &QPushButton::clicked , this , &Aircondition_Dialog::decreaseTemp);
    
    //模式显示
    ui->modeLabel->setText(modes[currentMode]);
    ui->modeLabel->setAlignment(Qt::AlignCenter);
    QFont modeFont = ui->modeLabel->font();
    modeFont.setPointSize(24);
    ui->modeLabel->setFont(modeFont);
    connect(ui->modeButton , &QPushButton::clicked , this , &Aircondition_Dialog::toggleMode);
    
    //风速显示
    ui->fanspeedLabel->setText(fanSpeeds[currentFanSpeed]);
    ui->fanspeedLabel->setAlignment(Qt::AlignCenter);
    ui->fanspeedLabel->setFont(modeFont);
    connect(ui->fanButton ,&QPushButton::clicked , this , &Aircondition_Dialog::toggleFanSpeed);
    
    //电源按钮
    connect(ui->powerpushButton ,&QPushButton::clicked , this , &Aircondition_Dialog::togglePower);
}

Aircondition_Dialog::~Aircondition_Dialog()
{
    delete ui;
}

void Aircondition_Dialog::toggleMode()
{
    currentMode = (currentMode + 1) % modes.size();
    ui->modeLabel->setText(modes[currentMode]);
    updateModeColor();
}

void Aircondition_Dialog::toggleFanSpeed()
{
    currentFanSpeed = (currentFanSpeed +1) % fanSpeeds.size();
    ui->fanspeedLabel->setText(fanSpeeds[currentFanSpeed]);
}

void Aircondition_Dialog::increaseTemp()
{
    if(currentTemp < 30)
    {
        currentTemp++;
        ui->tempLabel->setText(QString::number(currentTemp) + "℃");
    }
}

void Aircondition_Dialog::decreaseTemp()
{
    if(currentTemp > 16)
    {
        currentTemp--;
        ui->tempLabel->setText(QString::number(currentTemp) + "℃");
    }
}

void Aircondition_Dialog::updateModeColor()
{
    QString color;
    switch (currentMode) {
            case 0: color = "#66FF66"; break; 
            case 1: color = "#66B3FF"; break; 
            case 2: color = "#FF6666"; break; 
            case 3: color = "#CC7722"; break;
            case 4: color = "#1A2F4B"; break; 
    }
//    ui->modeLabel->setStyleSheet("color: " + color + ";");
    QPalette Palette = ui->modeLabel->palette();
    Palette.setColor(QPalette::Window , QColor("#E5E9EC"));
    Palette.setColor(QPalette::WindowText , QColor(color));
    ui->modeLabel->setAutoFillBackground(true);
    ui->modeLabel->setPalette(Palette);
}

void Aircondition_Dialog::togglePower() {
            isPowerOn = !isPowerOn;
            if(isPowerOn) {
                ui->powerpushButton->setIcon(QIcon(":/new/prefix1/电源_开"));
                ui->tempLabel->setEnabled(true);
                ui->modeLabel->setEnabled(true);
                ui->fanspeedLabel->setEnabled(true);
                ui->DownButton->setEnabled(true);
                ui->UpButton->setEnabled(true);
                ui->modeButton->setEnabled(true);
                ui->fanButton->setEnabled(true);
            } else {
                ui->powerpushButton->setIcon(QIcon(":/new/prefix1/电源_关"));
                ui->tempLabel->setEnabled(false);
                ui->modeLabel->setEnabled(false);
                ui->fanspeedLabel->setEnabled(false);
                ui->DownButton->setEnabled(false);
                ui->UpButton->setEnabled(false);
                ui->modeButton->setEnabled(false);
                ui->fanButton->setEnabled(false);
            }
}
